﻿using Presenters.WPF;
using Presenters.WPF.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AirlinePilotAcademy_WPF.UI_Administrator.Subject
{
    /// <summary>
    /// Lógica de interacción para SearchSubject.xaml
    /// </summary>
    public partial class SearchSubject : Window, ISearchSubject
    {
        SearchSubjectPresenter presenter;
        private string Message;
        public Model.Subject subject;

        public SearchSubject()
        {
            InitializeComponent();
            presenter = new SearchSubjectPresenter(this);
            presenter.SearchSubjectRealTime();
        }

        public SearchSubject(Model.Subject subject)
        {
            InitializeComponent();
            this.subject = subject;
            this.Button_AcceptSearchSubject.Visibility = Visibility.Visible;
            presenter = new SearchSubjectPresenter(this);
            presenter.SearchSubjectRealTime();
        }

        public bool isOnlyActive
        {
            get { return (this.CheckBox_OnlyUpdatedSubject.IsChecked == true); }
        }
        public string searchFilter
        {
            get { return (this.TextBox_Filter.Text); }
        }

        public bool isSearchByName
        {
            get { return this.RadioButton_ByName.IsChecked == true; }
        }
        public bool isSearchByCode
        {
            get { return this.RadioButton_ByCode.IsChecked == true; }
        }
        public bool isSearchByAcronym
        {
            get { return this.RadioButton_ByAcronym.IsChecked == true; }
        }
        public bool realTimeSearch
        {
            get { return (this.CheckBox_RealTimeSearch.IsChecked == true); }
        }

        public Collection<Model.Subject> results
        {
            set { this.DataGrid_Results.ItemsSource = value; }
        }
        public string message
        {
            set { Message = value; }
        }
        public Model.Subject selectedSubject
        {
            get { return (Model.Subject)this.DataGrid_Results.SelectedItem; }
        }

        private void CheckBox_uncheckedOnlyUpdatedSubject(object sender, RoutedEventArgs e)
        {
            if (presenter != null)
            {
                presenter.SearchSubjectRealTime();
            }
        }

        private void CheckBox_checkedOnlyUpdatedSubject(object sender, RoutedEventArgs e)
        {
            if (presenter != null)
            {
                presenter.SearchSubjectRealTime();
            }
        }

        private void CheckBox_checkedRealTimeSearch(object sender, RoutedEventArgs e)
        {
            this.Button_Search.IsEnabled = false;
        }

        private void CheckBox_uncheckedRealTimeSearch(object sender, RoutedEventArgs e)
        {
            this.Button_Search.IsEnabled = true;
        }

        private void RadioButton_SearchBy(object sender, RoutedEventArgs e)
        {
            if (presenter != null)
            {
                presenter.SearchSubjectRealTime();
            }
        }

        private void TextBox_FilterChanged(object sender, TextChangedEventArgs e)
        {
            if (presenter != null)
            {
                presenter.SearchSubjectRealTime();
            }
        }

        private void Button_Search_Click(object sender, RoutedEventArgs e)
        {
            if (presenter != null)
            {
                presenter.SearchSubject();
            }
        }

        private void Button_Click_ModifySubject(object sender, RoutedEventArgs e)
        {
            if (this.DataGrid_Results.SelectedItem != null)
            {
                SubjectSolution modififySubject = new SubjectSolution((Model.Subject)this.DataGrid_Results.SelectedItem);
                modififySubject.ShowDialog();
            }
        }

        private void DataGrid_SelectedItem(object sender, MouseButtonEventArgs e)
        {
            if (this.DataGrid_Results.SelectedItem != null)
            {
                this.Button_ModifySubject.IsEnabled = true;
                this.Button_WatchInfoSubject.IsEnabled = true;
                this.Button_AcceptSearchSubject.IsEnabled = true;
            }
        }

        private void Button_WatchInfoSubject_Click(object sender, RoutedEventArgs e)
        {
            if (this.DataGrid_Results.SelectedItem != null)
            {
                SubjectSolution viewInfoSubject = new SubjectSolution((Model.Subject)this.DataGrid_Results.SelectedItem, "Cerrar");
                viewInfoSubject.ShowDialog();
            }
        }

        private void Button_AceptSearchSubject_Click(object sender, RoutedEventArgs e)
        {
            this.subject = (Model.Subject)this.DataGrid_Results.SelectedItem;
            this.Close();
        }
    }
}
